﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XNAnimation;

namespace PloobsEngine.SceneControl
{
    public class CustomContentManager : IContentManager
    {
        public CustomContentManager(EngineStuff engine)
        {
            this.engine = engine;
        }

        private IDictionary<String, object> _modelDic = new Dictionary<String, object>(); 
        private EngineStuff engine;


        #region IContentManager Memberss

        private T LoadAsset<T>(String path)
            {
                T t = engine.Content.Load<T>(path);
                _modelDic.Add(path, t);
                return t;
        }

        #endregion

        #region IContentManager Members

        public T GetAsset<T>(string fileName)
        {            

            if (_modelDic.ContainsKey(fileName))
            {
                return (T) _modelDic[fileName];
            }
            else
            {
                return LoadAsset<T>(fileName);
            }

        }

        public ModelMesh GetModelParts(string fileName, int indexInModel)
        {
            if (_modelDic.ContainsKey(fileName))
            {
                Model m = (Model)_modelDic[fileName];
                return m.Meshes[indexInModel];
            }
            else
            {
                return LoadAsset<Model>(fileName).Meshes[indexInModel];
            }            
        }
      
        #endregion

        #region IContentManager Members

        public Matrix[] GetAnimatedTransformationMatrix(string fileName)
        {
               if (_modelDic.ContainsKey(fileName))
            {
                SkinnedModel m = (SkinnedModel)_modelDic[fileName];
                Matrix[] m2 = new Matrix[m.Model.Bones.Count];
                m.Model.CopyBoneTransformsTo(m2);
                return m2;                
            }
            else
            {
                SkinnedModel m = LoadAsset<SkinnedModel>(fileName);
                Matrix[] m2 = new Matrix[m.Model.Bones.Count];
                m.Model.CopyBoneTransformsTo(m2);
                return m2;                
            }            
        }

        public Microsoft.Xna.Framework.Matrix[] GetTransformationMatrix(string fileName)
        {
            if (_modelDic.ContainsKey(fileName))
            {
                Model m = (Model)_modelDic[fileName];
                Matrix[] m2 = new Matrix[m.Bones.Count];
                m.CopyBoneTransformsTo(m2);
                return m2;                
            }
            else
            {
                Model m = LoadAsset<Model>(fileName);
                Matrix[] m2 = new Matrix[m.Bones.Count];
                m.CopyBoneTransformsTo(m2);
                return m2;                
            }            
        }

        #endregion

        #region IContentManager Members

        
        #endregion
    }
}
